#!/bin/bash
rootDir=ohosmain
branch=master
target=rk3568
policy=newest # newest or configed

while getopts ":r:b:t:p:" opt
do
    case $opt in
        r)
        rootDir=$OPTARG
        ;;
        b)
        branch=$OPTARG
        ;;
        t)
        target=$OPTARG
        ;;
        p)
        policy=$OPTARG
        ;;
        ?)
        echo "未知参数"
        exit 1;;
        :)
        echo "没有输入任何选项"
        ;;
    esac
done

echo "The initial information: rootDir = $rootDir; branch = $branch; target = $target policy = $policy"

scriptPath=$(cd `dirname $0`; pwd)
runLog=${scriptPath}/run.log # log file
rootPath=${HOME}/Program/$rootDir # the path to store the code
codePath=${HOME}/Program/$rootDir/$rootDir # the path to store the code
repotool=`which repo`

if [ $(uname -s) == "Linux" ]; then
    cmdList[0]="rm -rf $rootPath"
    cmdList[1]="mkdir -p $codePath"
    cmdList[2]="cd $codePath"
    if [ $policy == "configed" ] && [ -f "${scriptPath}/manifest.xml" ]; then
        echo "There is the manifest.xml, Modify!"
        cmdList[3]="$repotool init -u https://gitee.com/openharmony/manifest.git -b $branch --no-repo-verify && cp ${scriptPath}/manifest.xml $codePath/.repo/manifests/ && $repotool init -m manifest.xml"
    else
        echo "Update the newest code."
        cmdList[3]="$repotool init -u https://gitee.com/openharmony/manifest.git -b $branch --no-repo-verify"
    fi
    cmdList[4]="$repotool sync -c"
    cmdList[5]="$repotool forall -c 'git lfs pull'"
    cmdList[6]="bash build/prebuilts_download.sh"
    cmdList[7]="$repotool start --all master"
    cmdList[8]="$repotool start --all dev"
    if [ $target == "sdk" ]; then
        cmdList[9]="./build.sh --product-name ohos-sdk --gn-args sdk_platform=win --gn-flags='--export-compile-commands'"
        cmdList[10]="mkdir .vscode"
        cmdList[11]="cp ${scriptPath}/settings_win.json .vscode/settings.json"
    else
        cmdList[9]="./build.sh --product-name rk3568 --ccache --gn-flags='--export-compile-commands'"
        cmdList[10]="mkdir .vscode"
        cmdList[11]="cp ${scriptPath}/settings_rk.json .vscode/settings.json"
        cmdList[12]="cd $codePath/out/rk3568/packages/phone/images"
        cmdList[13]="tar -I 'gzip --fast' -cf ../dayu200.tar.gz *"
    fi
elif [ $(uname -s) == "Darwin" ]; then
    cmdList[0]="rm -rf $rootPath"
    cmdList[1]="mkdir -p $codePath"
    cmdList[2]="cd $codePath"
    cmdList[3]="$repotool init -u https://gitee.com/openharmony/manifest.git -b $branch --depth=10 --no-repo-verify"
    cmdList[4]="$repotool sync -c"
    cmdList[5]="$repotool forall -c 'git lfs pull'"
    cmdList[6]="bash build/prebuilts_download.sh"
    cmdList[7]="$repotool start --all master"
    cmdList[8]="$repotool start --all dev"
    cmdList[9]="./build.sh --product-name ohos-sdk --gn-args full_mini_debug=false --gn-flags='--export-compile-commands'"
    cmdList[10]="mkdir .vscode"
    cmdList[11]="cp ${scriptPath}/settings.json .vscode/settings.json"
    # cmdList[9]="./build.sh --product-name ohos-sdk --gn-args full_mini_debug=false --gn-args enable_glfw_window=true"
fi

echo "-------------------------start-------------------------" > $runLog
for (( i = 0; i < ${#cmdList[@]}; i++))
do
    echo -e "The command is \"${cmdList[$i]}\"." >> $runLog
    eval ${cmdList[$i]} >> $runLog
    echo -e "\n\n" >> $runLog
done
echo "--------------------------end--------------------------" >> $runLog

